<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/20 0020
 * Time: 14:21
 */
namespace app\api\model;
use think\Db;
use think\Model;
use think\Request;

class RankingModel extends Model
{
    /**获取token和page和limit，返回好友排行数
     * @param Request $request
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public static function friends_rankings(Request $request){
        $token = $request->param('token',null);
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        if(!$token){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        if(!(UserModel::istoken($token))){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        $user = Db::name('p_management')->where('token',$token)->find();
        if($user['friend']){
            $openid = json_decode($user['friend']);
        }
        $openid[] = $user['openid'];
        $friend = Db::name('p_management')
            ->where('openid','in',$openid)
            ->order('total_integral desc')
            ->limit(($page-1)*$limit,$limit)
            ->select();
        foreach ($friend as $key=>$value){
            if($value==$user){
                $user['paiming'] = $key+1;
            }
        }
        return ['code'=>200,'data'=>$friend,'user'=>$user];
    }

    /**世界排行模型
     * @param Request $request
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public static function word_rankings(Request $request){
        $token = $request->param('token',null);
        if(!$token){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        if(!$user=UserModel::istoken($token)){
            return ['code'=>402,'msg'=>'参数错误'];
        }
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $rankings = Db::name('p_management')
            ->order('total_integral desc')
            ->limit(($page-1)*$limit,$limit)
            ->select();
        $user['paiming'] = UserModel::ranking($user);
//        return $user;
        return ['code'=>200,'data'=>$rankings,'user'=>$user];
    }
}